package com.techzhi.address_web.controller;

import com.techzhi.addres_score.model.AreaCode;
import com.techzhi.addres_score.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地址管理接口模块
 *
 * @author shouzhi
 */
@Slf4j
@RestController
@RequestMapping("/api/address")
public class AddressController {

    @Autowired
    private AddressService addressService;


    /**
     * 获取指定pcode所有地区列表接口
     *
     * @param pcode 区域父级code
     * @return 返回当前区域下的所有子级区域列表
     */
    @GetMapping("/areas/{pcode}")
    public Map<String, Object> getCities(@PathVariable Long pcode) {
        try {
            List<AreaCode> cities = addressService.getAreaByPCode(pcode);
            return createSuccessResult(cities);
        } catch (Exception e) {
            log.error("获取地址列表失败", e);
            return createErrorResult("获取地址列表失败");
        }
    }




//    @GetMapping("/provinces")
//    public Map<String, Object> getProvinces() {
//        try {
//            List<Province> provinces = addressService.getAllProvinces();
//            return createSuccessResult(provinces);
//        } catch (Exception e) {
//            log.error("获取省份列表失败", e);
//            return createErrorResult("获取省份列表失败");
//        }
//    }
//
//    @GetMapping("/cities/{provinceCode}")
//    public Map<String, Object> getCities(@PathVariable String provinceCode) {
//        try {
//            List<City> cities = addressService.getCitiesByProvince(provinceCode);
//            return createSuccessResult(cities);
//        } catch (Exception e) {
//            log.error("获取城市列表失败", e);
//            return createErrorResult("获取城市列表失败");
//        }
//    }
//
//    @GetMapping("/districts/{cityCode}")
//    public Map<String, Object> getDistricts(@PathVariable String cityCode) {
//        try {
//            List<District> districts = addressService.getDistrictsByCity(cityCode);
//            return createSuccessResult(districts);
//        } catch (Exception e) {
//            log.error("获取区县列表失败", e);
//            return createErrorResult("获取区县列表失败");
//        }
//    }
//
//    @GetMapping("/user/{userId}")
//    public Map<String, Object> getUserAddresses(@PathVariable Long userId) {
//        try {
//            List<UserAddress> addresses = addressService.getUserAddresses(userId);
//            return createSuccessResult(addresses);
//        } catch (Exception e) {
//            log.error("获取用户地址失败", e);
//            return createErrorResult("获取用户地址失败");
//        }
//    }
//
//    @PostMapping
//    public Map<String, Object> addAddress(@Valid @RequestBody UserAddress userAddress) {
//        try {
//            boolean result = addressService.addAddress(userAddress);
//            return result ? createSuccessResult(true) : createErrorResult("添加地址失败");
//        } catch (Exception e) {
//            log.error("添加地址失败", e);
//            return createErrorResult("添加地址失败");
//        }
//    }
//
//    @PutMapping("/default/{userId}/{addressId}")
//    public Map<String, Object> setDefaultAddress(@PathVariable Long userId, @PathVariable Long addressId) {
//        try {
//            boolean result = addressService.setDefaultAddress(userId, addressId);
//            return result ? createSuccessResult(true) : createErrorResult("设置默认地址失败");
//        } catch (Exception e) {
//            log.error("设置默认地址失败", e);
//            return createErrorResult("设置默认地址失败");
//        }
//    }
//
//    @DeleteMapping("/{addressId}")
//    public Map<String, Object> deleteAddress(@PathVariable Long addressId) {
//        try {
//            boolean result = addressService.deleteAddress(addressId);
//            return result ? createSuccessResult(true) : createErrorResult("删除地址失败");
//        } catch (Exception e) {
//            log.error("删除地址失败", e);
//            return createErrorResult("删除地址失败");
//        }
//    }

    private Map<String, Object> createSuccessResult(Object data) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "成功");
        result.put("success", true);
        result.put("data", data);
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }

    private Map<String, Object> createErrorResult(String message) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 500);
        result.put("msg", message);
        result.put("success", false);
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
}
